package com.bjb.vr.remote.ws.rabbitmq.handler;

import com.alibaba.fastjson.JSONObject;
import com.bjb.vr.common.constant.ThreadLocalConstant;
import com.bjb.vr.common.utils.ThreadLocalUtil;
import com.bjb.vr.remote.ws.rabbitmq.entity.RabbitWebSocketMsg;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;

/**
 * @version 1.0.0
 * @description:
 * @author: caihaibo
 * @createDate: 2022/3/30
 */

@Service
@Slf4j
public class MessageHandlerUtils {

    @Autowired
    public List<MessageHandler>  messageHandlers;

    public MessageHandlerUtils(){
    }

    public void process(String msg){

        if(StringUtils.isEmpty(msg)){
            log.error("msg is null");
            return;
        }

        RabbitWebSocketMsg rabbitWebSocketMsg = JSONObject.parseObject(msg,RabbitWebSocketMsg.class);
        //校验tenant_id
        ThreadLocalUtil.set(ThreadLocalConstant.NO_TENANT_ID_VERIFY, true);
        if(messageHandlers!=null && messageHandlers.size()>0){
            for(MessageHandler messageHandler:messageHandlers){
                messageHandler.process(rabbitWebSocketMsg);
            }
//            messageHandlers.stream().parallel().forEach(messageHandler -> messageHandler.process(rabbitWebSocketMsg));
        }
        msg = null;
        rabbitWebSocketMsg = null;
    }


}
